import axios from 'axios';
import qs from 'qs'
import router from '../router';
import {
	Message
} from 'element-ui'

// export const baseUrl = 'http://192.168.80.140:9005';
export const headerTitle = '泰基付款申请'
export const title = '襄阳泰基混凝土有限公司'

// production
export const baseUrl = 'http://taiji.stycie.com'

// export const baseUrl = 'http://119.96.100.253:9005';
// export const title = '襄阳****有限公司'
// export const headerTitle = '付款申请'


const service = axios.create({
	// baseURL: 'http://taiji.stycie.com',
	baseURL: baseUrl,
	
	timeout: 5000
});

service.interceptors.request.use(
	config => {
		if (config.method === 'post') {
			config.data = qs.stringify(config.data)
		}
		const userToken = localStorage.getItem("user-token")
		if (userToken) {
			config.headers.Authorization = "Bearer " + userToken
			config.headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
		}
		return config;
	},
	error => {
		return Promise.reject();
	}
);

service.interceptors.response.use(
	response => {
		if (response.status === 200) {
			if(response.data.status==200){
				return response.data;
			}else{
				Message({
					showClose: true,
					message: response.data.msg,
					type: 'error',
					duration: 2000
				})
				return  Promise.reject(response);
			}
		} else {
			Message({
				showClose: true,
				message: response.data.msg,
				type: 'error',
				duration: 2000
			})
			return  Promise.reject(response);
		}
	},
	error => {
		Message({
			showClose: true,
			message: '服务链接错误，请联系管理员！',
			type: 'error',
			duration: 2000
		})
		return Promise.reject()
	}
);

export default service;
